package tcp;/*

 */

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.util.UUID;

/**
 * @ClassName MyServerHandler
 * @Author Final
 * @Project DataStructures
 */
public class MyServerHandler extends  SimpleChannelInboundHandler<ByteBuf> {

    private  int count;
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {

        byte[] bytes = new byte[msg.readableBytes()];
        msg.readBytes(bytes);

        String string = new String(bytes,CharsetUtil.UTF_8);

        System.out.println(string);
        System.out.println(++this.count);

        ByteBuf buf = Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8);
        ctx.writeAndFlush(buf);
    }
}
